package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.CustomerTransaction;
import com.ruoyi.system.service.ICustomerTransactionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Customer;
import com.ruoyi.system.service.ICustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户Controller
 * 
 * @author ruoyi
 * @date 2024-12-12
 */
@RestController
@RequestMapping("/wms/customer")
public class CustomerController extends BaseController
{
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ICustomerTransactionService customerTransactionService;
    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(Customer customer)
    {
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Customer customer)
    {
        List<Customer> list = customerService.selectCustomerList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(customerService.selectCustomerById(id));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Customer customer)
    {
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Customer customer)
    {
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('wms:customer:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerService.deleteCustomerByIds(ids));
    }
    @PreAuthorize("@ss.hasPermi('wms:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping("/Ying")
    public AjaxResult Yingshou(@RequestBody Customer customer)
    {
        CustomerTransaction customerTransaction=new CustomerTransaction();
//        System.out.println(customer);
        Customer customer1 = customerService.selectCustomerById(customer.getId());
        customerTransaction.setPreviousBalance(customer1.getReceivableAmount());
        BigDecimal subtract = customer.getReceivableAmount().add(customer1.getReceivableAmount());
        customer1.setReceivableAmount(subtract);
        customerService.updateCustomer(customer1);
//        customerTransaction.setTransactionCode();
        customerTransaction.setCustomerId(customer.getCustomerNo());
        String time =String.valueOf(System.currentTimeMillis());
        customerTransaction.setTransactionCode(time);
        customerTransaction.setTransactionType("2");
        customerTransaction.setTransactionAmount(customer.getReceivableAmount());
        customerTransaction.setCurrentBalance(subtract);
        customerTransaction.setRemark(customer.getRemark());
        customerTransactionService.insertCustomerTransaction(customerTransaction);
        return null;
    }
    @PreAuthorize("@ss.hasPermi('wms:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping("/Jie")
    public AjaxResult Jiekuan(@RequestBody Customer customer)
    {
        CustomerTransaction customerTransaction=new CustomerTransaction();
//        System.out.println(customer);
        Customer customer1 = customerService.selectCustomerById(customer.getId());
        customerTransaction.setPreviousBalance(customer1.getReceivableAmount());
        BigDecimal subtract = customer1.getReceivableAmount().subtract(customer.getReceivableAmount());
        customer1.setReceivableAmount(subtract);
        customerService.updateCustomer(customer1);
//        customerTransaction.setTransactionCode();
        customerTransaction.setId(customer.getId());
        customerTransaction.setCustomerId(customer.getCustomerNo());
        String time =String.valueOf(System.currentTimeMillis());
        customerTransaction.setTransactionCode(time);
        customerTransaction.setTransactionType("1");
        customerTransaction.setTransactionAmount(customer.getReceivableAmount());
        customerTransaction.setCurrentBalance(subtract);
        customerTransaction.setRemark(customer.getRemark());
        customerTransactionService.insertCustomerTransaction(customerTransaction);
        return null;
    }
}
